
package com.windmill.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.windmill.bean.Card;
import com.windmill.bean.ChangeCard;
import com.windmill.bean.GroupUser;
import com.windmill.bean.SlotCard;
import com.windmill.bean.ThemeCard;
import com.windmill.bean.UserMainCardInfo;

public class CardUtil {

    /**
	 * @param main
	 * @param sourQQ
	 * @return 分析是否有价值保存的卡，并注上贡献者QQ
	 */
	public static ChangeCard analyse(UserMainCardInfo main, long sourQQ) {
		ChangeCard changeCard = new ChangeCard();
		if(System.currentTimeMillis()/1000-main.getLastrandti()>86400){
			for(SlotCard slotCard:main.getChangeboxcards()){
				Card card = slotCard.getCard();
				changeCard.getCards().add(card);
			}
			List<Card> accordCards = filterUsefulCard(changeCard.getCards(),main.getChangeThemes());			
			changeCard.setCards(accordCards);
			changeCard.setChangeThemes(main.getChangeThemes());
			changeCard.setLinkQQ(main.getQq());
			changeCard.setSaveQQ(sourQQ);		
		}			
		return changeCard;
	}

	/**
	 * @param cards
	 * @param themes
	 * @return 过滤卡
	 * themeType
	 *  0: normal 
		1: off  
		2: onSpecial 	
		5: activity
		9: flash = 12
	 */
	public static List<Card> filterUsefulCard(List<Card> cards,List<ThemeCard> themes) {
		Set<Integer> prices = getAllCardPrices(themes);
		List<Card> accordCards = new ArrayList<Card>();
		for(Card card:cards)
		{	/*绝版、活动、面值大于等于80*/
			if(card.getTheme()!=null&&card.getPrice()<9999
					&&(card.getTheme().getType()==1||card.getTheme().getType()==5||card.getPrice()>=80))
			{
				if((card.getPrice()%10!=0&&card.getTheme().getId()!=115) //特殊面值卡，排除（浓情年味）
						||prices.size()==0  //无箱锁
						||prices.contains(card.getPrice()))//箱锁有等面值卡交换
				{ 
					accordCards.add(card);
				}
			}
		}
		return accordCards;
	}

	/**
	 * @param list
	 * @return filter card price
	 */
	private static Set<Integer> getAllCardPrices(List<ThemeCard> themes) {
		Set<Integer> prices = new HashSet<Integer>();
		for(ThemeCard theme : themes){
			if(theme.getType()!=1){
				for(Card card:theme.getCards()){
					prices.add(card.getPrice());
				}
			}
		}
		if(themes.size()>0&&prices.size()==0){
			prices.add(-1);//绝版过滤,只有绝版锁，不让有面值相等的机会
		}
		return prices;
	}

	/**
	 * @param reciveUser
	 * @param payUser
	 * @param card 以卡片面值付money
	 * @return -1:交易失败，>=1交易成功
	 */
	public static int exchangeMoney(GroupUser reciveUser, GroupUser payUser,Card card) {
		int money = needMoney(card);		
		reciveUser.addMoney(money*0.9);
		payUser.addMoney(-money);	
		return money;
	}
	/**
	 * @param card
	 * @return 卡值多少魔法币
	 */
	public static int needMoney(Card card){
		int money = 0;
		if(card.getPrice()<=50){
			money = 1;
		}else if(card.getPrice()>40&&card.getPrice()<=200){
			money = 2;
		}else{
			money = 3;
		}
		if(card.getTheme().getType()==1||card.getTheme().getType()==5){
			money = money*2;
		}
		return money;
	}
	/**
	 * @param payUser
	 * @param card
	 * @return 是否有足够的魔法币
	 */
	public static boolean hasMoney(GroupUser payUser, Card card) {
		 int money = needMoney(card);		 
		return payUser.getMoney()-money>-10;
	}
}
